﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using CJ.SSOClient;
using IMT.Models;

namespace IMT.Controllers
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (System.Web.HttpContext.Current.Session[UserInfo.SESSION_KEY] == null)
            {
                System.Web.HttpContext.Current.Session[UserInfo.SESSION_KEY] = ReInitUser();
            }

            base.OnActionExecuting(filterContext);
        }

        public UserInfo ReInitUser()
        {
            string UserInfoString = GetUserInfo();

            if (UserInfoString == "")
            {
                return null;
            }
            UserInfo User = JsonConvert.DeserializeObject<UserInfo>(GetUserInfo());

            return User;
        }

        public string GetUserInfo()
        {
            if (Tools.GetCookie(Stands.UID) != "")
            {
                WebUtils web = new WebUtils();
                return web.DoGet(Stands.AUTH_HOST + "/Users/GetUserInfo?uid=" + Tools.GetCookie(Stands.UID), null);
            }
            else
            {
                return "";
            }
        }

    }
}
